import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import 'package:xlsk/config/easyLoading.dart';
import 'package:xlsk/config/index.dart';
import 'package:xlsk/routes/xlsk_router.dart';
import 'package:xlsk/store/model/userInfo.dart';

class Request {
  late Dio dio;
  String contentType;
  late BuildContext context;

  factory Request(BuildContext context,[contentType='application/x-www-form-urlencoded']){
    final _instance = Request.init(context,contentType);
    return _instance;
  }

  Request.init(context,this.contentType){
    this.context = context;
    final options = BaseOptions(
      baseUrl: '${Config().baseUrl}',
      headers: {
        "Content-Type":this.contentType
      },
    );
    this.dio = Dio(options);
    this.dio.interceptors.add(InterceptorsWrapper(this.context));
  }

  get(path,[queryParameters,mod=1]) async{
    queryParameters = queryParameters!=null?queryParameters:{};
    queryParameters['mod'] = mod;
    Response response = await this.dio.get(path,queryParameters:queryParameters);
    print('response ${path}');print(response.data);
    return response.data;
  }

  post(path,[data,mod=1]) async{
    data = data!=null?data:{};
    data['mod'] = mod;
    Response response = await this.dio.post(path,data:data);
    print('response ${path}');print(response.data);
    return response.data;
  }

}

class InterceptorsWrapper extends Interceptor {
  late BuildContext context;
  //  拦截器
  InterceptorsWrapper(this.context);

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async{
    var context = this.context;
    var provider_userinfo = Provider.of<UserInfo>(context,listen: false);
    var status = await provider_userinfo.checkInfo(context);
    if(status){
      if(options.data['mod'] == 2){
        options.queryParameters = {
          'token':provider_userinfo.userInfo['token']
        };
      }
      options.data['token'] = provider_userinfo.userInfo['token'];
    }
    options.data.remove('mod');
    return super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    var code = response.data['code'] is String?num.parse(response.data['code']):response.data['code'];
    if(code != 200){
      handleCode(response.data);
    }
    return super.onResponse(response, handler);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    return super.onError(err, handler);
  }

  void handleCode(response){
    var code = response['code'];
    var message = response['message'];
    print(code);
    switch(code){
      case 300:
      case "300":
        //全部移到home.dart进行判断(刚进入就判断)
        // SetEasyLoading(displayDuration: 1200);
        // EasyLoading.showToast('未登录');
        // XlskRouter.go(this.context, '/login');
      break;
      default:
        SetEasyLoading(displayDuration: 1200);
        EasyLoading.showToast(message);
      break;
    }
  }
}